<template>
    <div>
		<head-top></head-top>
        <br />
		<el-row>
			<el-col :span="0.5">&nbsp;</el-col>
			<el-col :span="20">
				<el-button type="primary" icon="el-icon-circle-plus"
					@click="dialogFormVisible=true">添加角色</el-button>
			</el-col>
		</el-row>
        <br />
		<el-dialog title="添加角色" width="300px"  :visible.sync="dialogFormVisible">
		  <el-form :model="form">
		    <el-form-item label="角色名称" :label-width="formLabelWidth">
		      <el-input v-model="form.rolename" auto-complete="off"></el-input>
		    </el-form-item>
		    <el-form-item label="是否启用" :label-width="formLabelWidth">
		      <el-select v-model="form.isstart">
		        <el-option label="启用" value="1"></el-option>
		        <el-option label="禁用" value="0"></el-option>
		      </el-select>
		    </el-form-item>
			<el-row>
				<el-form-item >
					<el-col :span="18">
						<el-button type="success" @click="submitForm('form')">确定</el-button>
					</el-col>
					<el-col :span="6">
						<el-button type="primary" @click="dialogFormVisible = false">取消</el-button>
					</el-col>
				</el-form-item>
			</el-row>
		  </el-form>
		</el-dialog>
		  <el-table
		    :data="tableData"
		    height="450"
		    border
		    style="width: 100%">
		    <el-table-column
		      prop="rolename"
		      label="角色名"
              align="center"
		      >
		    </el-table-column>
		    <el-table-column
		      prop="creationtime"
              align="center"
			  :formatter="dateFormat"
		      label="创建时间"
		      >
		    </el-table-column>
		    <el-table-column
            align="center"
		      label="是否启用"
			  >
			  <template slot-scope="scope">
				  <i v-if="scope.row.isstart===1" style="color: #11B95C;">启用</i>
				  <i v-else style="color: red;">禁用</i>
			  </template>
			  </el-table-column>
		    </el-table-column>
		    <el-table-column label="操作" align="center">
			  <template slot-scope="scope">
				<el-button
				  size="mini"
				  type="success"
				  @click="update(scope.row.id)"
				  >编辑</el-button>
				<el-button
				  size="mini"
				  type="danger"
				  @click="confirmDel(scope.row.id)">删除</el-button>
			  </template>
			</el-table-column>
		  </el-table>
		  <el-dialog title="修改角色" width="300px" :visible.sync="dialogTableVisible">
		    <el-form :model="form" >
		      <el-form-item label="角色名称" :label-width="formLabelWidth">
		        <el-input v-model="form.rolename" auto-complete="off"></el-input>
		      </el-form-item>
		      <el-form-item label="是否启用" :label-width="formLabelWidth">
		        <el-select v-model="form.isstart">
		          <el-option label="启用" value="1"></el-option>
		          <el-option label="禁用" value="0"></el-option>
		        </el-select>
		      </el-form-item>
			  <el-row>
				<el-form-item >
					<el-col :span="18">
						<el-button type="success" @click="submitTable('form')">确定</el-button>
					</el-col>
					<el-col :span="6">
						<el-button type="primary" @click="dialogTableVisible = false">取消</el-button>
					</el-col>
				</el-form-item>
			  </el-row>
		    </el-form>
		  </el-dialog>
	</div>
</template>

  <script>
	   import {mapState} from 'vuex'
	   import headTop from '../components/headTop'
	   import {queryRole,updateRole,queryRoleById,insertRole,deleteRole} from '@/api/getData'
	   import moment from 'moment/moment'
        export default {
			computed: {
			 	...mapState(['adminInfo']),
			 },
			components: {
				headTop,
			},
			data() {
			  return {
			    tableData: [],
				dialogTableVisible: false,
				dialogFormVisible: false,
				visible2: false,
				formLabelWidth: '80px',
				form: {
					  rolename: '',
					  isstart: '',
					  id:'',
					},
			  }
			},
			mounted(){
				this.queryRole();
			},
			methods:{
				async queryRole(){
					this.tableData=await queryRole();
				},
				dateFormat:function(row,column){
					var date = row[column.property];
			         if (date == undefined) {
			            return "";
			         }
			         return moment(date).format("YYYY-MM-DD");
				},
				async update(id){
					const role=await queryRoleById({id:id});
					this.form.rolename=role.rolename;
					this.form.id=id;
					this.dialogTableVisible = true;
				},
				async submitTable(formName) {
					this.dialogTableVisible = false;
					let params={rolename:this.form.rolename,isstart:this.form.isstart,id:this.form.id};
					const res=await updateRole({...params,userid:this.adminInfo.user.id,usercode:this.adminInfo.user.usercode});
					if (res.status == 1) {
						this.$message({
							type: 'success',
							message: res.message
						});

					}else{
						this.$message({
							type: 'error',
							message: res.message
						});
					}
					/*将所有data还原为初始状态*/
					Object.assign(this.$data, this.$options.data());
					/*重新查询数据*/
					this.queryRole();
			  },
			  async submitForm(formName) {
				    this.dialogFormVisible = false;
					let params={rolename:this.form.rolename,isstart:this.form.isstart,createdby:this.adminInfo.user.usercode};
					const res=await insertRole({...params,userid:this.adminInfo.user.id,usercode:this.adminInfo.user.usercode});
					if (res.status == 1) {
						this.$message({
							type: 'success',
							message: res.message
						});
						this.queryRole();
					}else{
						this.$message({
							type: 'error',
							message: "插入失败!"
						});
					}
			  },
			  confirmDel(id){
				 this.$confirm('确定删除该角色?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning'
						  }).then(() => {
							this.deleteRole(id);
						  }).catch(() => {
							this.$message({
							  type: 'info',
							  message: '已取消删除'
							});
						 });
			  },
			  async deleteRole(id){
				  const res=await deleteRole({id:id});
				  if (res.status == 1) {
				  	this.$message({
				  		type: 'success',
				  		message: res.message
				  	});
				  	this.queryRole();
				  }else{
				  	this.$message({
				  		type: 'error',
				  		message: "删除失败!"
				  	});
				  }
			  }
			}
		}
      </script>
	  <style>

	  </style>
